<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import wsjzglService from '@/api/wsjzgl'
import axios from '@/request/http'

const file = ref()
// 入参

// 生命周期
onMounted(() => {
  //
})

/*
 * 表单校验
 */
const formValidate = () => {
  //
}
/* 弹框状态 */
const dialogVisible = ref(false)
/*
 * 打开弹框
 */

const treeNodeData = ref()
const openDialog = (node: any) => {
  treeNodeData.value = node
  dialogVisible.value = true
}

/*
 * 关闭弹框
 */
const closeDialog = () => {
  //
  dialogVisible.value = false
}
/*
 * 下载目录模板
 */
const downMlmb = () => {
  window.location.href = axios.defaults.baseURL + '/api/wsjzgl/downMldrmb'
  // const params = {}
  // wsjzglService
  //   .downMldrmb(params)
  //   .then((res) => {
  //     ElMessage({
  //       type: 'success',
  //       message: '下载目录模板成功',
  //     })
  //     dialogVisible.value = false
  //   })
  //   .catch((err) => {
  //     !err.isError && ElMessage({ type: 'warning', message: err.message })
  //   })
}

// 点击校验上传文件
const upload = () => {
  file.value.multiple = false
  file.value.click()
}

const uploadFile = (e: any) => {
  const file = e.target.files[0]
  // if (!file.name.includes('pdf')) {
  //   ElMessage({ type: 'error', message: '只能上传pdf文件' })
  //   file.value.value = ''
  //   return
  // }
  const formData = new FormData()
  formData.append('ajbh', treeNodeData.value.ajbh)
  formData.append('file', file)
  const params = formData
  wsjzglService
    .importAjml(params)
    .then((res) => {
      // dialogVisible.value = false
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
}

/*
 * 保存（成功抛出刷新）
 */
const save = () => {
  const params = {}
  // wsjzglService
  //   .addPlxgjl(params)
  //   .then((res) => {
  //     //
  //     dialogVisible.value = false
  //     ElMessage({
  //       type: 'success',
  //       message: '导入目录成功',
  //     })
  //   })
  //   .catch((err) => {
  //     !err.isError && ElMessage({ type: 'warning', message: err.message })
  //   })
}

defineExpose({
  openDialog,
})
</script>
<template>
  <!-- 导入目录弹窗 -->
  <el-dialog
    v-model="dialogVisible"
    title="导入目录"
    width="50%"
    :close-on-click-modal="false"
    :before-close="closeDialog"
  >
    <el-link type="primary" @click="downMlmb">下载目录模板</el-link>
    <!-- <el-link type="primary">上传目录模板</el-link> -->
    <p>
      请下载目录模板，在模板中编辑目录信息后，先校验需要导入的目录文件是否符合规范，检验通过后点击确认上传完成导入工作
    </p>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="upload()">校 验</el-button>
        <!-- <el-button type="primary" @click="upload()">确认上传</el-button> -->
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>

    <input style="display: none" type="file" @change="uploadFile" ref="file" />
  </el-dialog>
</template>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>
